package com.jiyici.model;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import jiyici.Constant.ModelColumn;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * userId is unique in the whose system, id is unique for one user
 */
public abstract class ClientBaseModel implements Serializable {

    private static final long serialVersionUID = 6903192300329518315L;

    protected long id;
    
    public long userId;

    public long lastmodified;

    public boolean deleted;

    public boolean dirty;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    
    public void copyFrom(ClientBaseModel model) {
        id = model.id;
        userId = model.userId;
        lastmodified = model.lastmodified;
        deleted = model.deleted;
        dirty = model.deleted;
    }

    public ContentValues toContentValues() {
        ContentValues values = new ContentValues();
        values.put(ModelColumn.BASE_COLUMN_ID, id);
        values.put(ModelColumn.BASE_COLUMN_USERID, userId);
        values.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, lastmodified);
        values.put(ModelColumn.BASE_COLUMN_DELETED, deleted);
        values.put(ModelColumn.BASE_COLUMN_DIRTY, dirty);
        return values;
    }

    public void fromCursor(Cursor c) {
        id = c.getLong(c.getColumnIndex(ModelColumn.BASE_COLUMN_ID));
        userId = c.getLong(c.getColumnIndex(ModelColumn.BASE_COLUMN_USERID));
        lastmodified = c.getLong(c.getColumnIndex(ModelColumn.BASE_COLUMN_LASTMODIFIED));
        deleted = c.getInt(c.getColumnIndex(ModelColumn.BASE_COLUMN_DELETED)) > 0;
        dirty = c.getInt(c.getColumnIndex(ModelColumn.BASE_COLUMN_DIRTY)) > 0;
    }

    public void fromJson(JSONObject json) throws JSONException {
        id = json.getLong(ModelColumn.BASE_COLUMN_ID);
        userId = json.getLong(ModelColumn.BASE_COLUMN_USERID);
        lastmodified = json.getLong(ModelColumn.BASE_COLUMN_LASTMODIFIED);
        deleted = json.getBoolean(ModelColumn.BASE_COLUMN_DELETED);
        dirty = json.getBoolean(ModelColumn.BASE_COLUMN_DIRTY);
    }
    
    public JSONObject toJson() throws JSONException {
        JSONObject json = new JSONObject();
        json.put(ModelColumn.BASE_COLUMN_ID, id);
        json.put(ModelColumn.BASE_COLUMN_USERID, userId);
        json.put(ModelColumn.BASE_COLUMN_LASTMODIFIED, lastmodified);
        json.put(ModelColumn.BASE_COLUMN_DELETED, deleted);
        json.put(ModelColumn.BASE_COLUMN_DIRTY, dirty);
        return json;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (id ^ (id >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ClientBaseModel other = (ClientBaseModel) obj;
        if (id != other.id)
            return false;
        return true;
    }

}
